"use strict";

const mvc = require('cosjs.mvc');
const debug = mvc.library.require('debug');

exports.add = function (id) {
    if (!arguments[1]) {
        return;
    }
    let config = mvc.config('chip', id);
    if (!config) {
        return debug('config not exist', 'chip', id);
    }
    this.item.insert(id, arguments[1]);
    if (config['chipCollect'] != 0 && config['chipCollect'] != undefined) {
        this.collect.max(id, 1, 2);
    }
    if (config['chipKind'] == 1) {
        this.record.add(40, 1);
    } else {
        this.record.add(41, 1);
    }
    // this.record.add(93105+config.pos,1);
    //装备最高品质
    // this.record.max(1800,config.qlv);
    // switch(config.qlv){
    //     case 5:
    //     this.record.add(1900,1);
    //     break;
    //     case 6:
    //     this.record.add(2000,1);
    //     break;
    //     case 7:
    //     this.record.add(2100,1);
    //     break;

    // }
    // //成就 符文获取数量
    // this.record.add(93108, arguments[1])
}

exports.format = function (item, act) {
    mvc.format("chip", item, true);
    let config = mvc.config('chip', item.id);
    if (!config) {
        return debug('config not exist', 'chip', item.id);
    }
    // item.use = 'testAttr'
    //md强行区分两个分支啊。
    if (config['chipKind'] == 2) {
        //特殊芯片
        //从主属性里roll出3个不重复的属性加入到extraAttr里
        let attrConfig = mvc.config('chipAttr', config.mainGroupId);
        if (!attrConfig) {
            return debug('config not exist', 'chip', item.id);
        }
        //如果attrConfig里attr数组里id不为0的元素个数小于3，直接返回
        if (attrConfig.attr.length < 3) {
            return debug('config not exist', 'chip', item.id);
        }
        let count = 0;
        for (let i = 0; i < attrConfig.attr.length; i++) {
            if (attrConfig.attr[i].id != 0) {
                count++;
            }
        }
        if (count < 3) {
            return debug('config not exist', 'chip', item.id);
        }
        for (let i = 0; i < 3; i++) {
            let mainData = randExtraAttr(config.mainGroupId);
            //如果存在mainData.ID，重新随机
            while (item.extraAttr.hasOwnProperty(mainData.ID)) {
                mainData = randExtraAttr(config.mainGroupId);
            }
            item.extraAttr[mainData.ID] = mainData;
        }
        item.extraAttr["count"] = 0;
    } else {
        //普通芯片
        let masterAttrID = config.mainGroupId;
        let mainData = randAttr(masterAttrID)
        if (!mainData) {
            return debug('config not exist', 'chip');
        }
        item.mainAttr[mainData.ID] = mainData;

        //去除config.iniSubAttr里num为0的数据
        for (let value in config.iniSubAttr) {
            if (config.iniSubAttr[value].num == 0) {
                delete config.iniSubAttr[value];
            }
        }
        if (config.subAttrLimitation != 0) {
            let allPotin = 0;
            for (let value in config.iniSubAttr) {
                allPotin += config.iniSubAttr[value].weight;
            }
            //随机出副属性数量
            let randNum = Math.roll(0, allPotin);
            let attrNum = 0;
            let addSorce = 0;
            for (let value in config.iniSubAttr) {
                addSorce += config.iniSubAttr[value].weight
                if (randNum <= addSorce) {
                    attrNum = config.iniSubAttr[value].num;
                }
            }
            for (let i = 0; i < attrNum; i++) {
                let subAttrID = config.subGroupId;
                let subData = randAttr(subAttrID)
                if (subData) {

                }
                subData["lv"] = 0;
                item.otherAttr[subData.ID] = subData;
            }
        }
    }
    return item;
}

function randAttr(groupID) {
    let config = mvc.config('chipAttr', groupID);
    if (!config) {
        // return this.quick_error('config not exist', 'chip', groupID);
        return debug('config not exist', 'chip', groupID);
    }
    let attrID = 0;
    let attrValue = 0;
    let allPotin = 0;
    for (let value in config.attr) {
        allPotin += config.attr[value].weight;
    }
    let randNum = Math.roll(0, allPotin);
    let addSorce = 0;
    for (let value in config.attr) {
        addSorce += config.attr[value].weight
        if (randNum <= addSorce) {
            // console.log("value:",value);
            attrID = config.attr[value].id;
            let attrList = config.attr[value].value.split(',');
            attrValue = Math.roll(parseFloat(attrList[0]), parseFloat(attrList[1]));
            break;
        }

        //随机操作
    }
    var obj = new Object();
    obj.ID = attrID;
    obj.Value = attrValue;
    return obj;
}

function randExtraAttr(groupID) {
    let config = mvc.config('chipAttr', groupID);
    if (!config) {
        return debug('config not exist', 'chip', groupID);
    }
    let attrID = 0;
    let attrValue = 0;
    let allPotin = 0;
    for (let value in config.attr) {
        allPotin += config.attr[value].weight;
    }
    let randNum = Math.roll(0, allPotin);
    let addSorce = 0;
    for (let value in config.attr) {
        addSorce += config.attr[value].weight
        if (randNum <= addSorce) {
            // console.log("value:",value);
            attrID = config.attr[value].id;
            let attrList = config.attr[value].value.split(',');
            attrValue = Math.roll(parseFloat(attrList[0]), parseFloat(attrList[1]));
            break;
        }
        //随机操作
    }
    var obj = new Object();
    obj.ID = attrID;
    obj.Value = attrValue;
    obj.Data = new Array();
    return obj;
}